package com.redare.dev.smart.generator.sdk.pojo;

import com.redare.dev.smart.generator.sdk.parser.ParserUtils;
import com.redare.dev.smart.generator.sdk.utils.BuilderUtil;
import com.redare.devframework.common.utils.JsonUtils;
import org.apache.commons.lang.StringUtils;

import java.io.Serializable;
import java.sql.Types;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yangqiangyu
 * @date 2021-08-28 23:03:12
 * Created by yangqiangyu
 */
public class OnlFormField implements Serializable {

    /**
     * 自增主键
     */
    private Long id;

    /**
     * 表ID
     */
    private Long formHeadId;

    /**
     * 字段名字
     */
    private String dbFieldName;

    /**
     * 字段名称
     */
    private String field;

    /**
     * 首字母的大写的属性
     */
    private String firstUpperField;


    private String comment;

    /**
     * 原字段名
     */
    private String dbFieldNameOld;

    /**
     * 是否主键 0否 1是
     */
    private boolean dbIsKey;

    /**
     * 是否允许为空0否 1是
     */
    private boolean dbIsNull = true;


    /**
     * 数据库字段类型
     */
    private String dbType;

    /**
     * 数据库字段类型
     * @see Types
     */
    private Integer sqlType;

    /**
     * 属性类型
     */
    private String fieldType;

    /**
     * 数据库字段长度
     */
    private Integer dbLength;

    /**
     * 小数点
     */
    private Integer dbPointLength;

    /**
     * 表字段默认值
     */
    private String dbDefaultVal;

    /**
     * 字典code
     */
    private String dictField;

    /**
     * 字典表
     */
    private String dictTable;

    /**
     * 字典表对应实体
     */
    private String dictTableModel;

    /**
     * 字典取值
     */
    private String dictValue;

    /**
     * 字典Text
     */
    private String dictText;

    /**
     * 字段text赋值字段
     */
    private String dictTextAssignField;

    /**
     * 表单控件类型
     */
    private String fieldShowType;

    /**
     * 跳转URL
     */
    private String fieldHref;

    /**
     * 表单控件长度
     */
    private Integer fieldLength;

    /**
     * 表单字段校验规则
     */
    private String fieldValidType;

    /**
     * 字段是否必填
     */
    private boolean fieldMustInput;

    /**
     * 扩展参数JSON
     */
    private String fieldExtendJson;

    private Map fieldExtend;

    /**
     * 控件默认值，不同的表达式展示不同的结果。
     * 1. 纯字符串直接赋给默认值；
     * 2. #{普通变量}；
     * 3. {{ 动态JS表达式 }}；
     * 4. ${填值规则编码}；
     * 填值规则表达式只允许存在一个，且不能和其他规则混用。
     */
    private String fieldDefaultValue;

    /**
     * 是否查询条件0否 1是
     */
    private boolean query;

    /**
     * 表单是否显示0否 1是
     */
    private boolean showForm;

    /**
     * 列表是否显示0否 1是
     */
    private boolean showList;

    /**
     * 详情页是否显示 0否 1是
     */
    private boolean showInfo;



    /**
     * 是否是只读（1是 0否）
     */
    private boolean readOnly;

    /**
     * 查询模式
     */
    private String queryMode;

    /**
     * 外键主表名
     */
    private String mainTable;

    /**
     * 外键主键字段
     */
    private String mainField;

    /**
     * 排序
     */
    private Integer orderNum;

    /**
     * 修改人
     */
    private String updateBy;

    /**
     * 修改时间
     */
    private Date updateTime;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 创建人
     */
    private String createBy;

    /**
     * 自定义值转换器
     */
    private String converter;

    /**
     * 查询默认值
     */
    private String queryDefVal;

    /**
     * 查询配置字典text
     */
    private String queryDictText;

    /**
     * 查询配置字典code
     */
    private String queryDictField;

    /**
     * 查询配置字典table
     */
    private String queryDictTable;

    /**
     * 查询显示控件
     */
    private String queryShowType;

    /**
     * 是否启用查询配置1是0否
     */
    private String queryConfigFlag;

    /**
     * 查询字段校验类型
     */
    private String queryValidType;

    /**
     * 查询字段是否必填1是0否
     */
    private boolean queryMustInput;

    /**
     * 是否支持排序1是0否
     */
    private boolean sortFlag;


    public Long getId() {
        return id;
    }

    public OnlFormField setId(Long id) {
        this.id = id;
        return this;
    }

    public Long getFormHeadId() {
        return formHeadId;
    }

    public OnlFormField setFormHeadId(Long formHeadId) {
        this.formHeadId = formHeadId;
        return this;
    }

    public String getDbFieldName() {
        return dbFieldName;
    }

    public OnlFormField setDbFieldName(String dbFieldName) {
        this.dbFieldName = dbFieldName;
        return this;
    }

    public String getField() {
        return BuilderUtil.underlineToCamel(this.dbFieldName);
    }

    public OnlFormField setField(String field) {
        this.field = field;
        return this;
    }

    public String getFirstUpperField() {
        String str = BuilderUtil.underlineToCamel(this.dbFieldName);
        String substring = org.apache.commons.lang.StringUtils.substring(str, 0, 1);
        String s = org.apache.commons.lang.StringUtils.upperCase(substring) + org.apache.commons.lang.StringUtils.substring(str, 1);
        return s;
    }

    public OnlFormField setFirstUpperField(String firstUpperField) {
        this.firstUpperField = firstUpperField;
        return this;
    }

    public String getComment() {
        return comment;
    }

    public OnlFormField setComment(String comment) {
        this.comment = comment;
        return this;
    }

    public String getDbFieldNameOld() {
        return dbFieldNameOld;
    }

    public OnlFormField setDbFieldNameOld(String dbFieldNameOld) {
        this.dbFieldNameOld = dbFieldNameOld;
        return this;
    }

    public boolean isDbIsKey() {
        return dbIsKey;
    }

    public OnlFormField setDbIsKey(boolean dbIsKey) {
        this.dbIsKey = dbIsKey;
        return this;
    }

    public boolean isDbIsNull() {
        return dbIsNull;
    }

    public OnlFormField setDbIsNull(boolean dbIsNull) {
        this.dbIsNull = dbIsNull;
        return this;
    }

    public String getDbType() {
        return dbType;
    }

    public OnlFormField setDbType(String dbType) {
        this.setDbType(dbType, sqlType == null ? Types.NULL : sqlType);
        return this;
    }

    public void setDbType(String dbType, Integer sqlType) {
        this.dbType = dbType;
        this.sqlType = sqlType;

        FieldType fieldType = ParserUtils.getFieldType(dbType);
        if ("UNKNOWN".equals(fieldType.getJavaType())) {
            fieldType = ParserUtils.getFieldType(sqlType);
        }
        if (StringUtils.isBlank(this.fieldType)){
            this.fieldType = fieldType.getJavaType();
        }
    }


    public Integer getSqlType() {
        return sqlType;
    }

    public OnlFormField setSqlType(Integer sqlType) {
        this.sqlType = sqlType;
        return this;
    }

    public String getFieldType() {
        return fieldType;
    }

    public OnlFormField setFieldType(String fieldType) {
        this.fieldType = fieldType;
        return this;
    }

    public Integer getDbLength() {
        return dbLength;
    }

    public OnlFormField setDbLength(Integer dbLength) {
        this.dbLength = dbLength;
        return this;
    }

    public Integer getDbPointLength() {
        return dbPointLength;
    }

    public OnlFormField setDbPointLength(Integer dbPointLength) {
        this.dbPointLength = dbPointLength;
        return this;
    }

    public String getDbDefaultVal() {
        return dbDefaultVal;
    }

    public OnlFormField setDbDefaultVal(String dbDefaultVal) {
        this.dbDefaultVal = dbDefaultVal;
        return this;
    }

    public String getDictField() {
        return dictField;
    }

    public OnlFormField setDictField(String dictField) {
        this.dictField = dictField;
        return this;
    }

    public String getDictTable() {
        return dictTable;
    }

    public OnlFormField setDictTable(String dictTable) {
        this.dictTable = dictTable;
        return this;
    }

    public String getDictTableModel() {
        return BuilderUtil.underlineToCamel(this.dictTable, true);
    }

    public OnlFormField setDictTableModel(String dictTableModel) {
        this.dictTableModel = dictTableModel;
        return this;
    }

    public String getDictValue() {
        return dictValue;
    }

    public OnlFormField setDictValue(String dictValue) {
        this.dictValue = dictValue;
        return this;
    }

    public String getDictText() {
        return dictText;
    }

    public OnlFormField setDictText(String dictText) {
        this.dictText = dictText;
        return this;
    }

    public String getDictTextAssignField() {
        return dictTextAssignField;
    }

    public OnlFormField setDictTextAssignField(String dictTextAssignField) {
        this.dictTextAssignField = dictTextAssignField;
        return this;
    }

    public String getFieldShowType() {
        return fieldShowType;
    }

    public OnlFormField setFieldShowType(String fieldShowType) {
        this.fieldShowType = fieldShowType;
        return this;
    }

    public String getFieldHref() {
        return fieldHref;
    }

    public OnlFormField setFieldHref(String fieldHref) {
        this.fieldHref = fieldHref;
        return this;
    }

    public Integer getFieldLength() {
        return fieldLength;
    }

    public OnlFormField setFieldLength(Integer fieldLength) {
        this.fieldLength = fieldLength;
        return this;
    }

    public String getFieldValidType() {
        return fieldValidType;
    }

    public OnlFormField setFieldValidType(String fieldValidType) {
        this.fieldValidType = fieldValidType;
        return this;
    }


    public String getFieldExtendJson() {
        return fieldExtendJson;
    }

    public OnlFormField setFieldExtendJson(String fieldExtendJson) {
        this.fieldExtendJson = fieldExtendJson;
        return this;
    }

    public Map getFieldExtend() {
        if (fieldExtendJson == null) {
            return null;
        };
        return JsonUtils.parseJson(fieldExtendJson, HashMap.class);
    }

    public OnlFormField setFieldExtend(Map fieldExtend) {
        this.fieldExtend = fieldExtend;
        return this;
    }

    public String getFieldDefaultValue() {
        return fieldDefaultValue;
    }

    public OnlFormField setFieldDefaultValue(String fieldDefaultValue) {
        this.fieldDefaultValue = fieldDefaultValue;
        return this;
    }


    public String getQueryMode() {
        return queryMode;
    }

    public OnlFormField setQueryMode(String queryMode) {
        this.queryMode = queryMode;
        return this;
    }

    public String getMainTable() {
        return mainTable;
    }

    public OnlFormField setMainTable(String mainTable) {
        this.mainTable = mainTable;
        return this;
    }

    public String getMainField() {
        return mainField;
    }

    public OnlFormField setMainField(String mainField) {
        this.mainField = mainField;
        return this;
    }

    public Integer getOrderNum() {
        return orderNum;
    }

    public OnlFormField setOrderNum(Integer orderNum) {
        this.orderNum = orderNum;
        return this;
    }

    public String getUpdateBy() {
        return updateBy;
    }

    public OnlFormField setUpdateBy(String updateBy) {
        this.updateBy = updateBy;
        return this;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public OnlFormField setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
        return this;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public OnlFormField setCreateTime(Date createTime) {
        this.createTime = createTime;
        return this;
    }

    public String getCreateBy() {
        return createBy;
    }

    public OnlFormField setCreateBy(String createBy) {
        this.createBy = createBy;
        return this;
    }

    public String getConverter() {
        return converter;
    }

    public OnlFormField setConverter(String converter) {
        this.converter = converter;
        return this;
    }

    public String getQueryDefVal() {
        return queryDefVal;
    }

    public OnlFormField setQueryDefVal(String queryDefVal) {
        this.queryDefVal = queryDefVal;
        return this;
    }

    public String getQueryDictText() {
        return queryDictText;
    }

    public OnlFormField setQueryDictText(String queryDictText) {
        this.queryDictText = queryDictText;
        return this;
    }

    public String getQueryDictField() {
        return queryDictField;
    }

    public OnlFormField setQueryDictField(String queryDictField) {
        this.queryDictField = queryDictField;
        return this;
    }

    public String getQueryDictTable() {
        return queryDictTable;
    }

    public OnlFormField setQueryDictTable(String queryDictTable) {
        this.queryDictTable = queryDictTable;
        return this;
    }

    public String getQueryShowType() {
        return queryShowType;
    }

    public OnlFormField setQueryShowType(String queryShowType) {
        this.queryShowType = queryShowType;
        return this;
    }

    public String getQueryConfigFlag() {
        return queryConfigFlag;
    }

    public OnlFormField setQueryConfigFlag(String queryConfigFlag) {
        this.queryConfigFlag = queryConfigFlag;
        return this;
    }

    public String getQueryValidType() {
        return queryValidType;
    }

    public OnlFormField setQueryValidType(String queryValidType) {
        this.queryValidType = queryValidType;
        return this;
    }

    public boolean isSortFlag() {
        return sortFlag;
    }

    public OnlFormField setSortFlag(boolean sortFlag) {
        this.sortFlag = sortFlag;
        return this;
    }

    public boolean isFieldMustInput() {
        return fieldMustInput;
    }

    public OnlFormField setFieldMustInput(boolean fieldMustInput) {
        this.fieldMustInput = fieldMustInput;
        return this;
    }

    public boolean isQuery() {
        return query;
    }

    public OnlFormField setQuery(boolean query) {
        this.query = query;
        return this;
    }

    public boolean isShowForm() {
        return showForm;
    }

    public OnlFormField setShowForm(boolean showForm) {
        this.showForm = showForm;
        return this;
    }

    public boolean isShowList() {
        return showList;
    }

    public OnlFormField setShowList(boolean showList) {
        this.showList = showList;
        return this;
    }

    public boolean isShowInfo() {
        return showInfo;
    }

    public OnlFormField setShowInfo(boolean showInfo) {
        this.showInfo = showInfo;
        return this;
    }

    public boolean isReadOnly() {
        return readOnly;
    }

    public OnlFormField setReadOnly(boolean readOnly) {
        this.readOnly = readOnly;
        return this;
    }

    public boolean isQueryMustInput() {
        return queryMustInput;
    }

    public OnlFormField setQueryMustInput(boolean queryMustInput) {
        this.queryMustInput = queryMustInput;
        return this;
    }
}